package gapidt.common;

import gapidt.plugin.GapidtActivator;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class Locale {
	
	static Logger logger = Logger.getLogger(Locale.class);
	
	// Default language
	public static final String DEFAULT_LANGUAGE = "en";
	
	protected static String language = DEFAULT_LANGUAGE;
	protected static String loadedLanguage = "";

	// Messages file
	private static Properties messages = new Properties();
	
	public static String getLanguage() {
		return language;
	}

	public static void setLanguage(String language) {
		Locale.language = language;
	}

	protected static Properties getMessages(){
		if(!loadedLanguage.equals(language) || messages == null){
			try{
				messages = new Properties();
				messages.load(new FileInputStream(GapidtActivator.getFilePath("OSGI-INF/l10n/bundle"+((language==null || language==DEFAULT_LANGUAGE)?"":"_"+language)+".properties")));
				loadedLanguage = language;
			}catch(Exception e){
				logger.error(e);
			}
		}
		return messages;
	}
	
	/**
	 * Returns a message in the current language
	 */
	public static String getMessage(String messageKey){
		Properties messages = getMessages();
		if(messages != null && messages.get(messageKey) != null)
			return (String) messages.get(messageKey);
		return messageKey;
	}
}
